<%@ Page Language="C#" AutoEventWireup="false" MasterPageFile="ScreenMasterPage.master" %>
<asp:Content ContentPlaceHolderID="Head" runat="server">

	<script type="text/javascript">

		window.top.importCommonOshControl(this, "ChannelNumberInput.htc");
		window.top.importThemedOshControl(this, "TeletextPage.htc");

		var _teletext = osh.mediaController.getService("Teletext");
		var _inputHideTimer = new window.top.Timer("Teletext.inputHideTimer", 3000);
		_inputHideTimer.setAutoReset(false);
		_inputHideTimer.onTimeout.attach(null, onInputHideTimeout);
		
		function window::onload()
		{
			screen.onCommand.attach(this, onCommand);
			screen.onKeyPreview.attach(this, onKeyPreview);
			screen.onKey.attach(this, onKey);
			screen.onShow.attach(this, onShow);
			screen.onHide.attach(this, onHide);
		}
		function window::onunload()
		{
			screen.onCommand.detach(this, onCommand);
			screen.onKeyPreview.detach(this, onKeyPreview);
			screen.onKey.detach(this, onKey);
			screen.onShow.detach(this, onShow);
			screen.onHide.detach(this, onHide);
		}

		function onShow(sender, event)
		{
		}

		function onHide(sender, event)
		{
			setPageNumberInputMode(false);
		}

		function onKeyPreview(sender, event)
		{
			if(event.keyCode == window.top.Keys.VK_LEFT && !isPageNumberInputMode())
			{
				event.processed = true;
				_teletext.previousSubPage();
			}
		}

		function onKey(sender, event)
		{
			if(event.keyCode == window.top.Keys.VK_LEFT || event.keyCode == window.top.Keys.VK_CHANNELDOWN)
				_teletext.previousSubPage();
			else if(event.keyCode == window.top.Keys.VK_RIGHT || event.keyCode == window.top.Keys.VK_CHANNELUP)
				_teletext.nextSubPage();
			else if(event.keyCode == window.top.Keys.VK_UP || event.keyCode == window.top.Keys.VK_PRIOR)
				_teletext.previousPage();
			else if(event.keyCode == window.top.Keys.VK_DOWN || event.keyCode == window.top.Keys.VK_NEXT)
				_teletext.nextPage();
			else if(event.keyCode == window.top.Keys.VK_HOME)
				_teletext.gotoInitialPage();
			else if(event.keyCode == window.top.Keys.VK_RED)
				_teletext.followLink("red");
			else if(event.keyCode == window.top.Keys.VK_GREEN)
				_teletext.followLink("green");
			else if(event.keyCode == window.top.Keys.VK_YELLOW)
				_teletext.followLink("yellow");
			else if(event.keyCode == window.top.Keys.VK_BLUE)
				_teletext.followLink("blue");
			else if(event.keyCode == window.top.Keys.VK_ENTER && isPageNumberInputMode())
				onInputHideTimeout();
		}

		function onCommand(sender, event)
		{
			var command = event.command.toLowerCase();
			if(command == "")
			{
			}
		}
		
		function isPageNumberInputMode()
		{
			return pageNumber.className == "show";
		}
		
		function setPageNumberInputMode(setMode)
		{
			if(setMode == isPageNumberInputMode())
				return;
			
			if(setMode)
			{
				pageNumber.className = "show";
				_inputHideTimer.start();
			}
			else
			{
				pageNumber.value = 0;
				pageNumber.className = "hide";
				_inputHideTimer.stop();
			}
		}
		
		function onInputHideTimeout()
		{
			if(isPageNumberInputMode())
			{
				if(pageNumber.value >= 100 && pageNumber.value < 900)
					_teletext.gotoPage(pageNumber.value);
				setPageNumberInputMode(false);
				updateUI();
			}
		}

		function onPageNumberChange()
		{
			if(!isPageNumberInputMode())
				setPageNumberInputMode(true);
			else
			{
				if(pageNumber.value >= 100 && pageNumber.value < 900)
					onInputHideTimeout();
				else
					_inputHideTimer.restart();
			}
		}

		function updateUI()
		{
			if(screen.getVisible())
				osh.refresh();
		}
	</script>

</asp:Content>
<asp:Content ContentPlaceHolderID="Body" runat="server">

	<osh:TeletextPage id="teletext" onchange="updateUI()" />
	<osh:ChannelNumberInput id="pageNumber" format="{0}_" selected="true" onchange="onPageNumberChange()" />

</asp:Content>
